package com.woniuxy.microsysserver.mapper;

import com.woniuxy.microsysserver.bean.AdminInfo;
import com.woniuxy.microsysserver.bean.DepartmentInfo;
import com.woniuxy.microsysserver.bean.RoleInfo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

@Mapper
public interface AdminMapper {

    /**
     * 根据登录名，查询用户
     * @param loginName
     * @return
     */
    @Results({
            @Result(property = "departmentInfo",column = "fk_department",javaType = DepartmentInfo.class,one =
            @One(select = "com.woniuxy.microsysserver.mapper.DepartmentMapper.findById",fetchType = FetchType.LAZY)),
            @Result(property = "roleInfo",column = "fk_role",javaType = RoleInfo.class,one =
            @One(select = "com.woniuxy.microsysserver.mapper.RoleMapper.findById",fetchType = FetchType.LAZY))
    })
    @Select("select id,login_name as loginName,admin_name as adminName,state,password," +
            "fk_department,fk_role,phone,img from admin_info where login_name = #{loginName}")
    AdminInfo login(@Param("loginName") String loginName);

    /**
     * 新增管理员
     * @param adminInfo
     */
    @Insert("insert into admin_info (login_name,admin_name,fk_department,fk_role,create_time," +
            "state,phone,password) values (#{a.loginName},#{a.adminName},#{a.departmentInfo.id},#{a.roleInfo.id}," +
            "now(),1,#{a.phone},#{a.password})")
    void saveAdminInfo(@Param("a") AdminInfo adminInfo);

    /**
     * 查找所有管理员
     * @return
     */
    @Results({
            @Result(property = "departmentInfo",column = "fk_department",javaType = DepartmentInfo.class,one =
            @One(select = "com.woniuxy.microsysserver.mapper.DepartmentMapper.findById",fetchType = FetchType.LAZY)),
            @Result(property = "roleInfo",column = "fk_role",javaType = RoleInfo.class,one =
            @One(select = "com.woniuxy.microsysserver.mapper.RoleMapper.findById",fetchType = FetchType.LAZY))
    })
    @Select("select id,login_name as loginName,admin_name as adminName,fk_department,fk_role," +
            "create_time as createTime,phone from admin_info where state = 1")
    List<AdminInfo> findAll();

    /**
     * 按条件查询
     * @return
     */
    List<AdminInfo> findByCondition(@Param("a") AdminInfo adminInfo);

    @Update("update admin_info set state = 2 where id = #{id}")
    void delete(Integer id);

    /**
     * 修改管理员
     * @param adminInfo
     */
    void updateAdmin(@Param("a") AdminInfo adminInfo);


    @Select("select id,login_name as loginName,admin_name as adminName," +
            "phone from admin_info where state = 1 and fk_role = #{rid}")
    List<AdminInfo> findByRole(Integer rid);


    @Results({
            @Result(property = "departmentInfo",column = "fk_department",javaType = DepartmentInfo.class,one =
            @One(select = "com.woniuxy.microsysserver.mapper.DepartmentMapper.findById",fetchType = FetchType.LAZY)),
            @Result(property = "roleInfo",column = "fk_role",javaType = RoleInfo.class,one =
            @One(select = "com.woniuxy.microsysserver.mapper.RoleMapper.findById",fetchType = FetchType.LAZY))
    })
    @Select("select id,login_name as loginName,admin_name as adminName,fk_department,fk_role," +
            "create_time as createTime,phone from admin_info where id = #{id}")
    AdminInfo findById(Integer id);

    @Update("update admin_info set img = #{a.img} where id = #{a.id}")
    void changeHead(@Param("a") AdminInfo adminInfo);
}
